package com.xiaozhengkeji.mc.plugins.cdkey.web;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.io.InputStream;

public class XiaoZhengApiCloud {
    public static String API_BASE = "http://api.xiaozhengkeji.com:81/mc/";
    public static String PLUGIN_NAME = "CDKey";

    public static String getUPVersion() {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(API_BASE + PLUGIN_NAME + "/version.api");
        String strResult = "XiaoZhengCloud访问失败!";
        try {
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = entity.getContent();
                strResult = convertStreamToString(inputStream);
                request.abort();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return strResult;
    }

    public static String getNotice() {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(API_BASE + PLUGIN_NAME + "/notice.api");
        String strResult = "XiaoZhengCloud访问失败!";
        try {
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = entity.getContent();
                strResult = convertStreamToString(inputStream);
                request.abort();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strResult;
    }

    public static String getUPData() {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(API_BASE + PLUGIN_NAME + "/updata.api");
        String strResult = "XiaoZhengCloud访问失败!";
        try {
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = entity.getContent();
                strResult = convertStreamToString(inputStream);
                request.abort();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strResult;
    }

    public static String convertStreamToString(InputStream is) {
        StringBuilder sb1 = new StringBuilder();
        byte[] bytes = new byte[4096];
        int size = 0;
        try {
            while ((size = is.read(bytes)) > 0) {
                String str = new String(bytes, 0, size, "UTF-8");
                sb1.append(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb1.toString();
    }

}
